package edu.drexel.info636.assignments.controllers;

import java.util.InputMismatchException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * The main controller for this application, which reads and writes numbers to
 * and from a file via a CLI.
 * 
 * @author Chase Miller
 */
@Component
public class MainController extends ControllerBase {

    private static int READ_MODE = 1;
    private static int WRITE_MODE = 2;
    private static int MODIFY_MODE = 3;
    private static int QUIT = 4;

    @Autowired
    private FileController fileController;

    @Autowired
    private RecordListController recordController;

    /**
     * Instantiates a new instance of the
     * 
     * @see MainController class.
     * 
     * @param fileController
     *            - the system's instance of the file controller.
     */
    public MainController() {
    }

    /**
     * Starts the main controller, interacting with the user.
     */
    public void start() {
	System.out.println("*** Welcome to B2 *** \n");

	this.fileController.getFilePathFromUser();

	while (true) {
	    System.out.println("\n *** Main Menu ***");
	    // Continue based on the mode.
	    int mode = this.promptForMode();

	    if (mode == READ_MODE) {
		this.continueInReadMode();
	    } else if (mode == WRITE_MODE) {
		this.continueInWriteMode();
	    } else if (mode == MODIFY_MODE) {
		this.continueInModifyMode();
	    } else if (mode == QUIT) {
		System.out.println("Quitting the program...");
		System.exit(0);
	    } else {
		System.out.println("Invalid program mode detected.  Shutting down...");
		System.exit(0);
	    }
	}
    }

    /**
     * Prompts the user for the program mode and continues based on user input.
     * 
     * @return - Returns the mode
     */
    private int promptForMode() {
	int mode = 0; // initialize to 0, an invalid mode.

	// Keep prompting until a valid mode is supplied.
	while (mode != READ_MODE && mode != WRITE_MODE && mode != MODIFY_MODE && mode != QUIT) {
	    System.out.println("\nEnter one of the following numbers corresponding to a mode and press enter:");
	    System.out.println(READ_MODE + " to read from the file and display linear regression information.");
	    System.out.println(WRITE_MODE + " to write to the file");
	    System.out.println(MODIFY_MODE + " to modify the file");
	    System.out.println(QUIT + " to quit the program");

	    try {
		mode = Integer.parseInt(this.scan.nextLine());

		if (mode != READ_MODE && mode != WRITE_MODE && mode != MODIFY_MODE && mode != QUIT) {
		    // Throw an exception so that the error message can be
		    // placed in one spot: the catch clause.
		    throw new InputMismatchException();
		}
	    } catch (Throwable t) {
		// The user has entered an incorrect mode. Give the user a
		// chance to continue or quit.
		System.out.println("You have entered an incorrect mode.  Press r and enter to retry or anything else and enter to quit.");
		String choice = this.scan.nextLine();

		if (!choice.equalsIgnoreCase("r")) {
		    System.exit(0);
		}
	    }
	}

	return mode;
    }

    /**
     * Continues the application in read mode.
     */
    private void continueInReadMode() {
	System.out.println("** In read mode ** \n");

	this.recordController.readRecords();

	this.recordController.printRecordList();
    }

    /**
     * Continues the application in write mode.
     */
    private void continueInWriteMode() {
	System.out.println("** In write mode ** \n");

	this.recordController.populateRecordList();

	this.recordController.writeRecords(false);
    }

    /**
     * Continues the application in modify mode.
     */
    private void continueInModifyMode() {
	System.out.println("** In modify mode ** \n");

	this.recordController.modifyRecordList();
    }
}
